package com.legendframework.core.cmd.paramer.converter.impl;

import com.legendframework.core.ioc.IParameter;
import com.legendframework.core.ioc.annotation.Component;
import com.legendframework.core.cmd.exception.ParameterCastException;
import com.legendframework.core.cmd.paramer.converter.ParamerConverter;
import com.legendframework.core.support.lang3.StringUtils;
import org.bukkit.command.CommandSender;

import java.lang.reflect.Field;
import java.lang.reflect.Parameter;

@Component
public class EnumParamerConverter implements ParamerConverter<Enum> {

    @Override
    public boolean check(Class<?> cls) {
        return cls != null && cls.isEnum();
    }

    @Override
    public Enum castParameter(CommandSender sender, String message, Class<Enum> type, IParameter parameter) {
        Enum[] enumConstants = (Enum[]) type.getEnumConstants();
        for (Enum enumConstant : enumConstants) {
            if (StringUtils.equals(enumConstant.name(), message)) {
                return enumConstant;
            }
        }
        throw new ParameterCastException("可选范围: ["+StringUtils.join(enumConstants,",")+"]");
    }
}
